package com.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pojo.Medicine_info;
import com.pojo.Supplier_info;
import com.service.impl.Medicine_infoServiceImpl;
import com.service.impl.Supplier_infoServiceImpl;
import com.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述：
 * <p>
 * 作者：jiangjiahao
 * 时间：2023年06月28日 20:32
 */
@WebServlet(name = "Medicine_infoServlet", value = "/medicine_info")
public class Medicine_infoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("appliDogion/json;charset=utf-8");
        String tag = req.getParameter("tag");
        Medicine_infoServiceImpl medicine_infoService = new Medicine_infoServiceImpl();
        if ("search".equals(tag)){
            System.out.println("页面进入servlet");
            int index = 1;
            int size = 10;
            String query = "";
            if (req.getParameter("pageIndex") != null && req.getParameter("pageIndex") != ""){
                index = Integer.parseInt(req.getParameter("pageIndex"));
            }
            if (req.getParameter("pageSize") != null && req.getParameter("pageSize") != ""){
                size = Integer.parseInt(req.getParameter("pageSize"));
            }
            if (req.getParameter("query") != null && req.getParameter("query") !=""){
                query = req.getParameter("query");
            }
            PageUtil<Medicine_info> medicine_infoPageUtil = medicine_infoService.pagequery(index, size, query);
            System.out.println("查询到的药品信息集是："+medicine_infoPageUtil);
            String jsonstr = JSON.toJSONString(medicine_infoPageUtil, SerializerFeature.DisableCircularReferenceDetect);
            resp.getWriter().write(jsonstr);
            return;
        }else if ("del".equals(tag)){
            int id = Integer.parseInt(req.getParameter("id"));
            medicine_infoService.deleteMedicineInfoById(id);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("appliDogion/json;charset=utf-8");
        String tag = req.getParameter("tag");
        Medicine_infoServiceImpl medicine_infoService = new Medicine_infoServiceImpl();
        Supplier_infoServiceImpl supplier_infoService = new Supplier_infoServiceImpl();
        if ("add".equals(tag)){
            String name = req.getParameter("NAME");
            String category = req.getParameter("CATEGORY");
            int supplierid = Integer.parseInt(req.getParameter("supplier_info"));
            Supplier_info supplier = supplier_infoService.findSupplierById(supplierid);
            BigDecimal price = new BigDecimal(req.getParameter("UNIT_PRICE"));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date outtime = null;
            try {
                outtime = simpleDateFormat.parse(req.getParameter("EXPIRATION_DATE"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Medicine_info medicine_info = new Medicine_info(null, name, category, supplier, price, 0, outtime, 0);
            Integer integer = medicine_infoService.addMedicineInfo(medicine_info);
            if (integer == 0){
                resp.getWriter().write("success");
                return;
            }else {
                resp.getWriter().write("error");
                return;
            }
        }
    }
}

























